package koala.kuangjiademo.data;

import android.support.annotation.Nullable;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CheckStatusRecord implements Serializable {
    private Integer day;

    public CheckStatusRecord setDay(int day) {
        this.day = day;
        return this;
    }

    private static final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
    private String attendtime;
    public String out_address;
    public String in_address;
    @SerializedName("in_attendancestate")
    private int in_state;
    @SerializedName("out_attendancestate")
    private int out_state;
    @Nullable
    private Attendinfo attendinfo;

    public boolean hasData() {
        return attendtime != null;
    }

    @Nullable
    public String getCheckInTime() {
        return attendinfo != null ? attendinfo.in_time : null;
    }

    @Nullable
    public String getCheckOutTime() {
        return attendinfo != null ? attendinfo.out_time : null;
    }

    private static class Attendinfo {
        String out_time;
        String in_time;

        void initTime() throws ParseException {
            if (in_time != null) {
                in_time = in_time.substring(11);
            }
            if (out_time != null) {
                out_time = out_time.substring(11);
            }
        }
    }

    public Integer getCheckDay() {
        if (day == null) {
            try {
                Date date = CheckStatusRecord.date.parse(attendtime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                day = calendar.get(Calendar.DAY_OF_MONTH);
                if (attendinfo != null) attendinfo.initTime();
            } catch (ParseException e) {
                e.printStackTrace();
                day = 1;
            }
        }
        return day;
    }

    /**
     * @return null for ok status
     */
    @Nullable
    public String getCheckMessage(boolean in) {
        switch (in ? in_state : out_state) {
            case 1:
                return in ? "上班迟到" : "上班早退";
            case 2:
                return in ? "未签到" : "未签退";
            default:
                return null;
        }
    }

    public static class Response extends ResponsePacket<CheckStatusRecord[]> {
    }
}
